package 股票买卖4;

import java.io.*;
import java.util.Arrays;

public class Main {
    static int N = 100010;
    static int[][] f = new int[N][2];
    static int[] w = new int[N];

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] s = in.readLine().split(" ");
        int n = Integer.parseInt(s[0]);
        int m = Integer.parseInt(s[1]);
        String[] str = in.readLine().split(" ");
        for (int i = 0; i < n; i++) {
            w[i + 1] = Integer.parseInt(str[i]);
        }
        for (int i = 0; i < n; i++) {
            Arrays.fill(f[i], -0x3f3f3f3f);
        }
        for (int i = 1; i <= n; i++) {
            for (int j = m; j >= 1; j--) {
                f[i][0] = Math.max(f[i - 1][0], f[i - 1][1] + w[i]);
                f[i][1] = Math.max(f[i - 1][1], f[i - 1][0] - w[i]);
            }
        }
        int res = 0;
        for(int i = 1;i <= m;i ++) res = Math.max(res, f[i][0]);
        System.out.println(res);
    }
}